﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using BackEndApplication.Models;

namespace BackEndApplication.Controllers
{
    public class UserController : ApiController
    {
        private SQLMVC2008Entities db = new SQLMVC2008Entities();

        // GET api/User
        public IEnumerable<tbl_user> Gettbl_user()
        {
            var tbl_user = db.tbl_user.Include(t => t.tb_Location);
            return tbl_user.AsEnumerable();
        }

        // GET api/User/5
        public tbl_user Gettbl_user(string id)
        {
            tbl_user tbl_user = db.tbl_user.Find(id);
            if (tbl_user == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return tbl_user;
        }

        // PUT api/User/5
        public HttpResponseMessage Puttbl_user(string id, tbl_user tbl_user)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != tbl_user.Username)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(tbl_user).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/User
        public HttpResponseMessage Posttbl_user(tbl_user tbl_user)
        {
            if (ModelState.IsValid)
            {
                db.tbl_user.Add(tbl_user);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, tbl_user);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = tbl_user.Username }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/User/5
        public HttpResponseMessage Deletetbl_user(string id)
        {
            tbl_user tbl_user = db.tbl_user.Find(id);
            if (tbl_user == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.tbl_user.Remove(tbl_user);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, tbl_user);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}